package com.csc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.csc.constants.Constants;
import com.csc.domain.User;
import com.csc.service.UserService;

@Controller
public class LoginController {

	@Autowired
	private UserService userService;

	static final Logger logger = Logger.getLogger(LoginController.class);

	@RequestMapping(value = "/tologin", method = RequestMethod.GET)
	public String sentoLoginPage(HttpServletRequest request) {
		BasicConfigurator.configure();
		logger.info("send to login page");
		return "login";
	}

	// @author:ThanhBinh
	// action: go to user support main page searchAccount.jsp
	@RequestMapping(value = "/SupportPage", method = RequestMethod.POST)
	public String sendtoSupportPage(HttpServletRequest request) {
		logger.info("send to support page");
		return "searchAccount";
	}

	// action: go to user support main page viewTransaction.jsp
	@RequestMapping(value = "/ViewTransaction", method = RequestMethod.GET)
	public String sendtoViewTransaction(HttpServletRequest request) {
		logger.info("send to support page");
		return "viewTransaction";
	}

	// ADD
	// action: go to user support page addFunds.jsp
	@RequestMapping(value = "/AddFunds", method = RequestMethod.GET)
	public String sendtoAddFunds(HttpServletRequest request) {
		logger.info("send to support page");
		return "addFunds";
	}

	// action: go to user support page addConfirm.jsp
	@RequestMapping(value = "/AddConfirm", method = RequestMethod.GET)
	public String sendtoAddConfirm(HttpServletRequest request) {
		logger.info("send to support page");
		return "addConfirm";
	}

	// action: go to user support page addComplete.jsp
	@RequestMapping(value = "/AddComplete", method = RequestMethod.GET)
	public String sendtoAddComplete(HttpServletRequest request) {
		logger.info("send to support page");
		return "addComplete";
	}

	// WITHDRAW
	// action: go to user support page WithdrawFunds.jsp
	@RequestMapping(value = "/WithdrawFunds", method = RequestMethod.GET)
	public String sendtoWithdrawFunds(HttpServletRequest request) {
		logger.info("send to support page");
		return "withdrawFunds";
	}

	// action: go to user support page WithdrawConfirm.jsp
	@RequestMapping(value = "/WithdrawConfirm", method = RequestMethod.GET)
	public String sendtoWithdrawConfirm(HttpServletRequest request) {
		logger.info("send to support page");
		return "withdrawConfirm";
	}

	// action: go to user support page WithdrawComplete.jsp
	@RequestMapping(value = "/WithdrawComplete", method = RequestMethod.GET)
	public String sendtoWithdrawComplete(HttpServletRequest request) {
		logger.info("send to support page");
		return "withdrawComplete";
	}

	// TRANSFER
	// action: go to user support page TransferFunds.jsp
	@RequestMapping(value = "/TransferFunds", method = RequestMethod.GET)
	public String sendtoTransferFunds(HttpServletRequest request) {
		logger.info("send to support page");
		return "transferFunds";
	}

	// action: go to user support page TransferConfirm.jsp
	@RequestMapping(value = "/TransferwConfirm", method = RequestMethod.GET)
	public String sendtoTransferConfirm(HttpServletRequest request) {
		logger.info("send to support page");
		return "transferConfirm";
	}

	// action: go to user support page TransferComplete.jsp
	@RequestMapping(value = "/TransferComplete", method = RequestMethod.GET)
	public String sendtoTransferComplete(HttpServletRequest request) {
		logger.info("send to support page");
		return "transferComplete";
	}

	// ADMIN
	// action: go to user admin main page adminReviewTransaction.jsp
	@RequestMapping(value = "/AdminPage", method = RequestMethod.POST)
	public String sendtoAdminPage(HttpServletRequest request) {
		logger.info("send to admin page");
		return "redirect:/adminReviewTransaction.html";
	}

	// action: go to user admin page adminApprove.jsp
	@RequestMapping(value = "/AdminApprove", method = RequestMethod.GET)
	public String sendtoAdminApprove(HttpServletRequest request) {
		logger.info("send to admin page");
		return "adminApproveTransactionPage";
	}

	// action: go to user admin page AdminReject.jsp
	@RequestMapping(value = "/AdminReject", method = RequestMethod.GET)
	public String sendtoAdminReject(HttpServletRequest request) {
		logger.info("send to admin page");
		return "adminRejectTransactionPage";
	}

	// action: go to user admin page AdminView.jsp
	@RequestMapping(value = "/AdminView", method = RequestMethod.GET)
	public String sendtoAdminView(HttpServletRequest request) {
		logger.info("send to admin page");
		return "adminViewTransactionPage";
	}

	// action: check login
	@RequestMapping(value = "/Home", method = RequestMethod.POST)
	public String checkLoginPage(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		logger.info("method check login is called");
		String message = "";

		// call session
		HttpSession session = request.getSession();
		// get username and password
		String username = request.getParameter("userid");
		String password = request.getParameter("password");

		// validate username and password
		if (username.equals("") || password.equals("")) {
			message = "Please login again!";
			model.addAttribute("accountError", message);
		}
		// call login service
		// LoginService login = new LoginService();

		// use login service to check login
		// input:username and password
		// output: User if exist
		User user = userService.checkLogin(username, password);

		if (null != user) {
			session.setAttribute("user", user);
			if (user.getIdRole().getNameRole().equals(Constants.USER_SUPPORT)) {
				session.setAttribute("usertype", Constants.USER_SUPPORT);
				return "forward:/SupportPage.html";
			} else if (user.getIdRole().getNameRole()
					.equals(Constants.USER_ADMIN)) {
				session.setAttribute("usertype", Constants.USER_ADMIN);
				return "forward:/AdminPage.html";
			}
		}
		logger.info("method check login is ended");
		return "login";

	}

	// logout
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logOutMethod(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		session.invalidate();
		return "redirect:/";
	}

	// end @authorThanhBinh
}
